package com.enjoyor.soa.traffic.server.tsms.handler;

import com.enjoyor.soa.traffic.core.tsms.domain.MsgInfo;
import com.enjoyor.soa.traffic.core.tsms.domain.TaskExecuteRecord;
import com.enjoyor.soa.traffic.core.tsms.enums.EnumDicPublic;
import com.enjoyor.soa.traffic.server.tsms.common.RehearseGlobal;
import com.enjoyor.soa.traffic.server.tsms.dubbo.impl.DubboSchemeActionService;
import com.enjoyor.soa.traffic.util.handler.BasicMsgHandler;
import com.enjoyor.soa.traffic.util.helper.TimeHelper;

import java.util.HashMap;
import java.util.Map;

/**
 * 接收数据，数据清理
 */
public class AlarmDataDelayHandler extends BasicMsgHandler<MsgInfo> {
	private AlarmDataDelayHandler() {
		super(1000,"其他检查");
	}

	private static AlarmDataDelayHandler single;

	public static AlarmDataDelayHandler getInstance() {
		if (single == null) {
			synchronized (AlarmDataDelayHandler.class) {
				if (single == null) {
					single = new AlarmDataDelayHandler();
					single.setMultiProcess(5, 20, 20);
				}
			}
		}
		return single;
	}


	@Override
	protected boolean task(int taskTimes){
        return true;
    }
	
	@Override
	protected boolean process(MsgInfo msg) {
		  long delay=(msg.getReceiveTime().getTime() - msg.getUpdateTime().getTime())/1000;
		if (delay> DubboSchemeActionService.delayTime) {
			com.alibaba.fastjson.JSONObject jsonObject2 = new com.alibaba.fastjson.JSONObject();
			Map<String, Object> positionWranMap = new HashMap();
			String content = "定位设备"+ RehearseGlobal.ActionDataPool.get(msg.getExecuteTaskId()).get("positionDeviceId")+"延迟"+delay+"秒，请检查设备网络情况！";
			positionWranMap.put("type", "warn");
			positionWranMap.put("recordTime", TimeHelper.dateToLongString());
			positionWranMap.put("clash", content);
			positionWranMap.put("content", content);
			jsonObject2.put(RehearseGlobal.ActionDataPool.get(msg.getExecuteTaskId()).get("routeId").toString(), positionWranMap);
			InitializeHandler.getInstance().getServerPushServiceImpl().pushMessage("" + msg.getExecuteTaskId(), jsonObject2);

			//记录开始执行信息
			TaskExecuteRecord taskExecuteRecord = new TaskExecuteRecord();
			taskExecuteRecord.setExecuteTaskId(msg.getExecuteTaskId());
			taskExecuteRecord.setIsWarn("1");
			taskExecuteRecord.setRecordTypeId(EnumDicPublic.WRAN_POSITION_DELAY.getKey());
			taskExecuteRecord.setRecordTypeName(EnumDicPublic.WRAN_POSITION_DELAY.getValue());
			taskExecuteRecord.setRecordContent(content);
			InitializeHandler.getInstance().getTaskExecuteRecordService().insertRecord(taskExecuteRecord, "执行报警");
		}
		return true;
	}
}
